using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Project.Application;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace Project.Api {
    [DependsOn (
        typeof (AbpAspNetCoreMvcModule),
        typeof (AbpAutofacModule),
        typeof (ProjectApplicationModule)
    )]
    public class ProjectApiModule : AbpModule {
        public override void ConfigureServices (ServiceConfigurationContext context) {
            var services = context.Services;

            services.AddCors (co => co.AddPolicy ("any", cpb => cpb.AllowAnyHeader ().AllowAnyMethod ().AllowAnyOrigin ()));
            services.AddControllers ();
            services.AddSwaggerGen (c => {
                c.SwaggerDoc ("v1", new OpenApiInfo { Title = "Project.Api", Version = "v1" });
            });
        }

        public override void OnApplicationInitialization (ApplicationInitializationContext context) {
            var env = context.GetEnvironment ();
            var app = context.GetApplicationBuilder ();

            if (env.IsDevelopment ()) {
                app.UseDeveloperExceptionPage ();
                app.UseSwagger ();
                app.UseSwaggerUI (c => c.SwaggerEndpoint ("/swagger/v1/swagger.json", "Project.Api v1"));
            }

            app.UseHttpsRedirection ();

            app.UseRouting ();

            app.UseCors ("any");

            app.UseAuthorization ();

            app.UseEndpoints (endpoints => {
                endpoints.MapControllers ();
            });
        }
    }
}